package com.oracle.controller;

import com.oracle.comment.Constants;
import com.oracle.pojo.Category;
import com.oracle.pojo.Commodity;
import com.oracle.pojo.Image;
import com.oracle.pojo.User;
import com.oracle.service.CategoryService;
import com.oracle.service.CommodityService;
import com.oracle.service.ShoppingCartService;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CommController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CommodityService commService;
    @Autowired
    private ShoppingCartService cartService;
    @RequestMapping("/categorys")
    @ResponseBody
    public Object findCategoryByPid(Integer pid){
        return categoryService.findCategoryByPid(pid);
    }
    @RequestMapping("/save")
    public String save(ModelMap model, Commodity comm, String commImgOne, String commImgTwo, String commImgThree, String commImgFour, String commImgFive){
        commService.saveComm(comm);
        comm.setUrl(commImgOne);
        commService.update(comm);
        commService.saveImage(comm.getId(),commImgOne);
        commService.saveImage(comm.getId(),commImgTwo);
        commService.saveImage(comm.getId(),commImgThree);
        commService.saveImage(comm.getId(),commImgFour);
        commService.saveImage(comm.getId(),commImgFive);
        model.put("comm",comm);
        model.put("commImgOne",commImgOne);
        model.put("commImgTwo",commImgTwo);
        model.put("commImgThree",commImgThree);
        model.put("commImgFour",commImgFour);
        model.put("commImgFive",commImgFive);
        return "detail";
    }
    @RequestMapping("/commList")
    @ResponseBody
    public Object queryByCid(){
        List<Category> list = categoryService.findCategoryByPid(0);
        List<Map<String,Object>> result = new ArrayList<>();
        for(Category category:list){
           List<Category> child = category.getChildren();
           for(Category c:child){
               Map<String,Object> map = new HashMap<>();
               map.put("name",c.getName());
               List<Commodity> comm = commService.queryByCid(c.getId());
               map.put("comm",comm);
               result.add(map);
           }
        }
        return result;
    }
    @RequestMapping("/detail")
    public String detail(ModelMap model,int id) {
        Commodity comm = commService.findCommodityById(id);
        model.put("comm",comm);
        List<Image> list = commService.findImageByCid(id);
        model.put("commImgOne",list.get(0).getUrl());
        model.put("commImgTwo",list.get(1).getUrl());
        model.put("commImgThree",list.get(2).getUrl());
        model.put("commImgFour",list.get(3).getUrl());
        model.put("commImgFive",list.get(4).getUrl());
        return "detail";
    }
    @RequestMapping("/car")
    public String car(){
        return "car";
    }

}
